package com.maxus.mini.open.api.controller.fav;

import java.util.List;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.maux.mini.data.api.fav.FavService;
import com.maux.mini.data.api.lib.LibService;
import com.maux.mini.data.api.redis.RedisService;
import com.maux.mini.data.api.topic.TopicService;
import com.maux.mini.data.api.user.UserService;
import com.maxus.mini.common.Constants;
import com.maxus.mini.common.utils.BeanUtilsExtended;
import com.maxus.mini.common.vo.ResBody;
import com.maxus.mini.entity.fav.Fav;
import com.maxus.mini.entity.lib.Lib;
import com.maxus.mini.entity.topic.Topic;
import com.maxus.mini.entity.user.User;
import com.maxus.mini.entity.vo.fav.FavVo;
import com.maxus.mini.open.api.controller.BaseController;
import com.maxus.mini.open.api.vo.req.P100004Vo;
import com.maxus.mini.open.api.vo.req.P100005Vo;
import com.maxus.mini.open.api.vo.req.P100007Vo;
import com.maxus.mini.open.api.vo.req.P800007Vo;
import com.maxus.mini.open.api.vo.res.PR100004Vo;
import com.maxus.mini.open.api.vo.res.PR100005Vo;
import com.maxus.mini.open.api.vo.res.PR100007Vo;
import com.maxus.mini.open.api.vo.res.PR800007Vo;

/**
 * 收藏controller
 * 
 * @author wujinsong
 *
 */
@RestController()
@RequestMapping(value = "api/v1.0/fav")
public class FavController extends BaseController {
	@Reference(version = "1.0.0")
	private FavService<Lib, Lib> favService;
	@Reference(version = "1.0.0")
	private RedisService redisService;
	@Reference(version = "1.0.0")
	private UserService<User, User> userService;
	@Reference(version = "1.0.0")
	private TopicService<Topic, Topic> topicService;
	@Reference(version = "1.0.0")
	private LibService<Lib, Lib> libService;

	/**
	 * 添加收藏
	 * 
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "addMaxFav")
	@ResponseBody()
	public ResBody addMaxFav(P100004Vo req) {
		PR100004Vo retVo = new PR100004Vo();
		User user = userService.find(req.getUserId());

		Fav fav = new Fav();

		fav.setUser(user);
		favService.save(fav);

		return retVo;
	}

	/**
	 * 删除收藏
	 * 
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "removeMaxFav")
	@ResponseBody()
	public ResBody removeMaxFav(P100005Vo req) {
		PR100005Vo retVo = new PR100005Vo();

		Fav fav = favService.find(req.getFavId());
		favService.delete(fav);

		return retVo;
	}

	/**
	 * 添加收藏
	 * 
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "maxamf")
	@ResponseBody()
	public ResBody maxamf(P100007Vo req) {
		PR100007Vo retVo = new PR100007Vo();
		User user = userService.find(req.getUserId());

		Lib lib = libService.find(req.getLibId());

		Fav fav = new Fav();
		fav.setUser(user);
		fav.setLib(lib);

		favService.save(fav);

		return retVo;
	}

	/**
	 * 获取收藏列表
	 * 
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "maxlmf")
	@ResponseBody()
	public ResBody maxlmf(P800007Vo req) {
		PR800007Vo retVo = new PR800007Vo();
		List<Fav> libs = null;

		String key = Constants.CKEY_FACE_FAV_LIST + req.getUserId();
		if (redisService.getList(key, Fav.class) != null) {
			libs = redisService.getList(key, Fav.class);
		}

		if (libs != null) {
			retVo.setItemVos(libs);

			return retVo;
		}

		FavVo vo = new FavVo();
		try {
			BeanUtilsExtended.copyProperties(vo, req);
			libs = favService.page(vo).getContent();
			redisService.setList(key, libs);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}

		retVo.setItemVos(libs);

		return retVo;
	}
}
